package com.four.hospital.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.four.common.utils.R;
import com.four.common.vo.UserTo;
import com.four.hospital.dao.UserDao;
import com.four.hospital.entity.User;
import com.four.hospital.service.UserService;
import com.four.hospital.util.JwtUtil;
import com.four.hospital.vo.DoctorAndUserVo2;
import com.four.hospital.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author:wsy
 * @Date: 2022/01/07/20:38
 * @Description
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private StringRedisTemplate redisTempaleate;
    @Autowired
    private JwtUtil jwtUtil;
    /**
     *
     * 患者管理列表、分页、精准
     *
     * @param vo
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<User> userList(UserVo vo, Integer page, Integer size) {

        Page<User> userPage = new Page<>(page, size);
        return userDao.userList(userPage,vo);
    }

    /**
     * 患者管理详情查
     *
     * @return
     */
    @Override
    public DoctorAndUserVo2 findList(Integer userId) {
        return userDao.findList(userId);
    }

    @Override
    public R getYzmByPhone(String userPhone) {
        UserTo user = userDao.getByPhone(userPhone);
        if (user==null) {
            return R.error(200,"登录错误，请重新检查");
        }
        Random random = new Random();
        String yzm = "";
        int math = 6;
        for(int i = 0;i <= math;i++){

            yzm+=random.nextInt(10);
        }
        redisTempaleate.boundValueOps("four:hospital:user:yzm:"+userPhone).set(yzm,2, TimeUnit.MINUTES);
        return R.ok("验证码发送成功");
    }

    @Override
    public R login(User user) {
        String yzm = redisTempaleate.boundValueOps("four:hospital:user:yzm:"+user.getUserPhone()).get();
        if (yzm != null && !yzm.equals(user.getYzm())) {
            return R.error(500, "登录失败,验证码错误");
        }
        UserTo byPhone = userDao.getByPhone(user.getUserPhone());
        if (byPhone==null) {
            return R.error(500,"登录失败,手机号错误");
        }
        if (!user.getUserPwd().equals(byPhone.getUserPwd())) {
            return R.error(500,"登录失败,密码错误");
        }
        redisTempaleate.delete("four:hospital:user:yzm:"+user.getUserPhone());
        String token = jwtUtil.createJWT(byPhone.getUserId().toString(), byPhone.getUserPwd(), "login");
        redisTempaleate.boundValueOps(token).set(JSON.toJSONString(byPhone),30, TimeUnit.MINUTES);
        return R.ok("登录成功").put("token",token);
    }

    /**
     * 根据手机号查询
     * @param userPhone
     */
    @Override
    public void findByUserPhone(String userPhone) {
        userDao.findUserByPhone(userPhone);
    }
}